﻿// This project type requires the F# PowerPack at http://fsharppowerpack.codeplex.com/releases
// Learn more about F# at http://fsharp.net
// Original project template by Jomo Fisher based on work of Brian McNamara, Don Syme and Matt Valerio
// This posting is provided "AS IS" with no warranties, and confers no rights.

open System
open Microsoft.FSharp.Text.Lexing

open Ast
open Lexer
open Parser
open System.IO

printfn "TinyJavaCompiler"

let token_to_string (t:token) = 
  match t with 
  | EOF  -> "EOF" 
  | PLUS  -> "PLUS" 
  | MINUS  -> "MINUS" 
  | MUL  -> "MUL" 
  | DIV  -> "DIV" 
  | MOD  -> "MOD" 
  | AND  -> "AND" 
  | OR  -> "OR" 
  | LOWER  -> "LOWER" 
  | LOWEREQ  -> "LOWEREQ" 
  | GREATER  -> "GREATER" 
  | GREATEREQ  -> "GREATEREQ" 
  | EQ  -> "EQ" 
  | NOTEQ  -> "NOTEQ" 
  | NOT  -> "NOT" 
  | ASSIGN  -> "ASSIGN" 
  | CLASS  -> "CLASS" 
  | EXTENDS  -> "EXTENDS" 
  | VOID  -> "VOID" 
  | TY_INT  -> "TY_INT" 
  | TY_BOOLEAN  -> "TY_BOOLEAN" 
  | TY_STRING  -> "TY_STRING" 
  | RETURN  -> "RETURN" 
  | IF  -> "IF" 
  | ELSE  -> "ELSE" 
  | UAIL  -> "UAIL" 
  | WHILE  -> "WHILE" 
  | BREAK  -> "BREAK" 
  | CONTINUE  -> "CONTINUE" 
  | NEW  -> "NEW" 
  | LENGTH  -> "LENGTH" 
  | TRUE  -> "TRUE" 
  | FALSE  -> "FALSE" 
  | NULL  -> "NULL" 
  | THIS  -> "THIS" 
  | BRA  -> "BRA" 
  | KET  -> "KET" 
  | SQBRA  -> "SQBRA" 
  | SQKET  -> "SQKET" 
  | CURBRA  -> "CURBRA" 
  | CURKET  -> "CURKET" 
  | SEMICOLON  -> "SEMICOLON" 
  | COMMA  -> "COMMA" 
  | DOT  -> "DOT" 
  | STRING _ -> "STRING" 
  | UID s -> "UID : " + s
  | LID s -> "LID : " + s
  | FLOAT n -> "FLOAT : " + string n
  | INT32 n -> "INT32 : " + string n

let rec find_tokens w =
  match Lexer.tokenize w with
  | token.EOF -> ()
  | alpha ->
    Console.WriteLine(token_to_string alpha)
    find_tokens w

let rec readAndProcess() =
    let fin = File.ReadAllText("Program.tyj");
    match Console.ReadLine() with
    | "quit" -> ()
    | expr ->
        try
            printfn "Lexing [%s]" expr
            let lexbuff = LexBuffer<char>.FromString(fin)
            do find_tokens lexbuff
            
        with ex ->
            printfn "Unhandled Exception: %s" ex.Message

        //readAndProcess()

readAndProcess()